<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <script>
            /*
      思路：层序遍历每一层节点，判断是否对称即可
      
      */
            let tree = {
                val: 1,
                left: {
                    val: 2,
                    left: {
                        val: 4,
                    },
                    right: {
                        val: 5,
                    },
                },
                right: {
                    val: 3,
                    right: {
                        val: 7,
                    },
                },
            }
            //含有null的层序遍历。只需要多一次判断即可，就是node不存在push null
            function levelOrder(root) {
                if (!root) return []
                let result = []
                let queue = [root]
                while (queue.length) {
                    let len = queue.length
                    result.push([])
                    for (let i = 0; i < len; i++) {
                        let node = queue.shift()
                        if (!node) {
                            result[result.length - 1].push(0)
                        } else {
                            result[result.length - 1].push(node.val)
                            queue.push(node.left)
                            queue.push(node.right)
                        }
                    }
                }
                let newResult = result.map((item) => {
                    return item.join('')
                })
                let sss = newResult.every((item) => {
                    return isPali(item)
                })
                function isPali(item) {
                    let str = item.split('').reverse().join('')
                    if (str == item) {
                        return true
                    } else {
                        return false
                    }
                }
            }
            levelOrder(tree)
            //也可以用JSON.stringify

            //不含null的层序遍历
            var isSymmetric = function (root) {
                function levelOrder(root) {
                    if (!root) return []
                    let result = []
                    let queue = [root]
                    while (queue.length) {
                        let len = queue.length
                        result.push([])
                        for (let i = 0; i < len; i++) {
                            let node = queue.shift()
                            result[result.length - 1].push(node.val)
                            if (node.left) {
                                queue.push(node.left)
                            }
                            if (node.right) {
                                queue.push(node.right)
                            }
                        }
                    }
                }
            }
        </script>
    </body>
</html>
